﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Library;

namespace OA.Exam
{
    public partial class QustionMaintain : System.Web.UI.Page
    {
        Exam myExam = new Exam();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTypes();//绑定题型
                //修改题目
                if (Request["qId"] != null)
                {
                    HiddenMark.Value = "edit";
                    string questionId = Request["qId"].ToString();
                    DdlType.SelectedValue = myExam.GetRecordOfQuestion(questionId).Rows[0]["ItemType"].ToString();
                    contentSubject.Value = myExam.GetRecordOfQuestion(questionId).Rows[0]["Subject"].ToString();
                }
            }
        }

        protected void BindTypes()
        {
            DdlType.DataSource = Exam.GetQuestionTypes();
            DdlType.DataBind();
        }


        protected void BtnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("QuestionsManage.aspx");
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            string subject = contentSubject.Value;
            int score = 0;
            string questionType = DdlType.SelectedValue.ToString();

            if (HiddenMark.Value == "edit")
            {
                string questionId = Request["qId"].ToString();
                if (myExam.UpdateQuestionItems(questionId, questionType, subject, score))
                {
                    Response.Redirect("SetQuestionItems.aspx?qId=" + questionId);
                }
                else
                {
                    LabPrompt.Text = "保存失败";
                }
            }
            else
            {
                string questionId = SqlUtil.GetPrimaryKeyId();

                if (myExam.InsertQuestion(questionId, questionType, subject, score))
                {
                    Response.Redirect("SetQuestionItems.aspx?qId=" + questionId);
                }
                else
                {
                    LabPrompt.Text = "保存失败";
                }
            }
        }

    }
}
